home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / VideoToolbox 95.04.18 / VideoToolboxSources / GetScreenDevice.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-15  |  6.8 KB  |  229 lines  |  [TEXT/MMCC]

  1. /* GetScreenDevice.c
  2.  
  3.     device=GetScreenDevice(n);
  4. returns a handle to the n-th screen, where the MainDevice is the zero-th screen. 
  5. This is similar, but not identical, to the screen number in the Monitors
  6. Control panel.
  7.  
  8.     n=GetScreenIndex(device);
  9. returns the inverse of GetScreenDevice(). 
  10.  
  11.     slot=GetDeviceSlot(device);
  12. gets the "slot" for any screen device. 
  13.  
  14.     device=SlotToScreenDevice(n);
  15. returns a handle to the screen device in slot n. 
  16.  
  17.     device=GetWindowDevice(window);
  18. returns GDHandle of screen with largest intersection with the window's content.
  19.  
  20.     GDHandle    GetDeviceByRefNum(int n);
  21.     GDHandle    GetRectDevice(Rect *r);
  22.  
  23.     void        LocalToGlobalRect(Rect *r);
  24.     void        GlobalToLocalRect(Rect *r);
  25.  
  26. Copyright © 1989-1995 Denis G. Pelli
  27. HISTORY:
  28. 3/20/90        dgp    make compatible with MPW C.
  29. 3/22/90    dgp    changed GetDeviceSlot to use the AuxDCEHandle instead of deducing it
  30.             from the baseAddr of the PixMap. This is a cleaner way to do it.
  31. 4/9/90    dgp    eliminated #define for Mainscrn mispelling in Color.h
  32. 10/17/90 dgp Added AddressToScreenDevice() for compatibility with built-in video on
  33.             the Mac IIci, IIsi, and LC.
  34. 10/18/90 dgp Added LocalToGlobalRect() and GetWindowDevice().
  35. 8/24/91        dgp    Made compatible with THINK C 5.0.
  36. 2/1/92    dgp    Fixed bugs in GetWindowDevice() which resulted in returning garbage GDHandle.
  37. 3/3/92    dgp    In GetScreenDevice(), skip inactive screens.
  38. 8/20/92    dgp    expanded comments of GetDeviceSlot(), noting that it works even with
  39.             built-in video, e.g. on Mac IIci.
  40. 8/26/92    dgp    GetDeviceSlot() now returns -1 if none, since zero is a legal slot.
  41.             GetScreenDevice() first checks for 8-bit QuickDraw().
  42. 9/10/92    dgp    Actually implemented the 8/26 change instead of just changing the 
  43.             documentation. Oops.
  44. 4/17/93    dgp Deleted obsolete AddressToSlot and AddressToScreenDevice.
  45. 5/21/93    dgp    Fixed GetWindowDevice() to support GWorld's.
  46. 8/14/93    dgp    Based on answer from DEVSUPPORT, I cleaned up GetWindowDevice().
  47. 4/11/94    dgp    Added GetRectDevice() based on code extracted from GetWindowDevice().
  48. 11/30/94 dgp added GetDeviceByRefNum().
  49. */
  50. #include "VideoToolbox.h"
  51. GDHandle GetRectDevice(Rect *r);
  52. GDHandle GetDeviceByRefNum(int n);
  53.  
  54. GDHandle GetScreenDevice(int n)
  55. // Returns a handle to the n-th screen, where the MainDevice is the zero-th screen.
  56. // Returns NULL if request can't be satisfied.
  57. {
  58.     GDHandle device;
  59.     int i,error;
  60.     long value;
  61.  
  62.     if(n<0)return NULL;
  63.     error=Gestalt(gestaltQuickdrawVersion,&value);
  64.     if(error || value<gestalt8BitQD)return NULL;    // need 8-bit quickdraw
  65.     if(n==0) return GetMainDevice();
  66.     device=GetDeviceList();
  67.     i=0;
  68.     while(device!=NULL){
  69.         if (TestDeviceAttribute(device,screenDevice)
  70.             && !TestDeviceAttribute(device,mainScreen)
  71.             && TestDeviceAttribute(device,screenActive)){
  72.                 i++;
  73.                 if(i==n)break;
  74.             }
  75.         device = GetNextDevice(device);
  76.     }
  77.     return device;
  78. }
  79.  
  80. GDHandle GetDeviceByRefNum(int n)
  81. {
  82.     int j;
  83.     GDHandle device;
  84.  
  85.     for(j=0;;j++){
  86.         device=GetScreenDevice(j);
  87.         if(device==NULL || n==(**device).gdRefNum)break;
  88.     }
  89.     return device;
  90. }
  91.  
  92. int GetScreenIndex(GDHandle device)
  93. // Inverse of GetScreenDevice(). Returns -1 if request can't be satisfied.
  94. {
  95.     int i,error;
  96.     long value;
  97.  
  98.     error=Gestalt(gestaltQuickdrawVersion,&value);
  99.     if(error || value<gestalt8BitQD)return 0;
  100.     if(device==NULL)return -1;
  101.     for(i=0;i<16;i++)if(device==GetScreenDevice(i))return i;
  102.     return -1;
  103. }
  104.  
  105. short int GetDeviceSlot(GDHandle device)
  106. // Gets the "slot" for any screen device, even if it's built-in video, e.g. on Mac
  107. // IIci or Quadra. See 1992 Inside Mac "Processes" page 4-11. Returns -1 if none.
  108. // Zero is a legal slot for built-in video devices.
  109. {
  110.     AuxDCEHandle myAuxDCEHandle;
  111.  
  112.     if(device == NULL) return -1;
  113.     myAuxDCEHandle=(AuxDCEHandle) GetDCtlEntry((**device).gdRefNum);
  114.     return ((**myAuxDCEHandle).dCtlSlot);
  115. }
  116.  
  117. GDHandle SlotToScreenDevice(int n)
  118. // Returns a handle to the screen device in slot n.
  119. // Returns NULL if request can't be satisfied.
  120. {
  121.     GDHandle device;
  122.  
  123.     device=GetDeviceList();
  124.     while(device!=NULL) {
  125.         if (TestDeviceAttribute(device,screenDevice) &&
  126.             GetDeviceSlot(device)==n)
  127.                 break;
  128.         device=GetNextDevice(device);
  129.     }
  130.     return device;
  131. }
  132.  
  133. GDHandle GetWindowDevice(WindowPtr window)
  134. // For on-screen window, returns GDHandle of screen with largest intersection with the 
  135. // window's content.
  136. // For off-screen window (i.e. GWorld), it returns the GDHandle of the associated device.
  137. {
  138.     Rect r;
  139.     WindowPtr oldWindow;
  140.     long qD;
  141.  
  142.     if(window==NULL)return NULL;
  143.     Gestalt(gestaltQuickdrawVersion,&qD);
  144.     if(qD>=gestalt32BitQD && (((CWindowPtr)window)->portVersion&0xc001)==0xc001){
  145.         // It's a GWorld iff the portVersion has both high bits set (cGrafPort) and
  146.         // the low bit set (GWorld). See Apple Tech Note “RowBytes Revealed II”.
  147.         return GetGWorldDevice((GWorldPtr)window);
  148.     }
  149.     r=window->portRect;
  150.     GetPort(&oldWindow);
  151.     SetPort(window);
  152.     LocalToGlobalRect(&r);
  153.     SetPort(oldWindow);
  154.     return GetRectDevice(&r);
  155. }
  156.  
  157. GDHandle GetRectDevice(Rect *r)
  158. // Returns GDHandle of screen with largest intersection with the global rect.
  159. {
  160.     Rect overlap;
  161.     GDHandle device,dominantDevice=NULL;
  162.     long area,greatestArea;
  163.     long qD;
  164.  
  165.     if(r==NULL)return NULL;
  166.     Gestalt(gestaltQuickdrawVersion,&qD);
  167.     if(qD<gestalt8BitQD)return NULL;    // need 8-bit quickdraw
  168.     device=GetDeviceList();
  169.     greatestArea=0;
  170.     while(device!=NULL){
  171.         if(TestDeviceAttribute(device,screenDevice)
  172.             && TestDeviceAttribute(device,screenActive)){
  173.                 SectRect(r,&(*device)->gdRect,&overlap);
  174.                 area=(long)(overlap.right-overlap.left)*(overlap.bottom-overlap.top);
  175.                 if(area>greatestArea){
  176.                     greatestArea=area;
  177.                     dominantDevice=device;
  178.                 }
  179.             }
  180.         device=GetNextDevice(device);
  181.     }
  182.     return dominantDevice;
  183. }
  184.  
  185. void LocalToGlobalRect(Rect *r)
  186. {
  187.     Point pt={0,0};
  188.     
  189.     LocalToGlobal(&pt);
  190.     OffsetRect(r,pt.h,pt.v);
  191. }
  192.  
  193. void GlobalToLocalRect(Rect *r)
  194. {
  195.     Point pt={0,0};
  196.     
  197.     GlobalToLocal(&pt);
  198.     OffsetRect(r,pt.h,pt.v);
  199. }
  200.  
  201. #if UNIVERSAL_HEADERS
  202.     #include <Displays.h>
  203. #else
  204.     enum{kDMDriverNotDisplayMgrAwareErr = -6228};
  205.     extern pascal OSErr DMSetDisplayMode(GDHandle theDevice,unsigned long mode
  206.         ,unsigned long *depthMode,unsigned long reserved,Handle displayState)
  207.          ={0x303C,0x0A11,0xABEB};
  208.     extern pascal OSErr DMCheckDisplayMode(GDHandle theDevice,unsigned long mode
  209.         ,unsigned long depthMode,unsigned long *switchFlags,unsigned long reserved
  210.         ,Boolean *modeOk)={0x303C,0x0C12,0xABEB};
  211.     enum {
  212.         gestaltDisplayMgrAttr        = 'dply',    /* Display Manager attributes */
  213.         gestaltDisplayMgrPresent    = 0            /* True if Display Mgr is present */
  214.     };
  215. #endif
  216.  
  217. /* 
  218. These enum definitions for use with the cscGetNextResolution driver status call 
  219. are taken from the A7 draft (Feb 10,'95) of Apple's Designing PCI Cards and Drivers for
  220. Power Mac Computers. Undoubtedly these enum definitions will appear in some future 
  221. version of Video.h, perhaps with CodeWarrior 6, at which time they can be deleted from here.
  222. */
  223. enum{
  224.     kDisplayModeIDCurrent=0
  225.     ,kDisplayModeIDInvalid=0xffffffff
  226.     ,kDisplayModeIDFindFirstResolution=0xfffffffe
  227.     ,kDisplayModeIDNoMoreResolutions=0xfffffffd
  228. };
  229.